/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>
 
using System;
using System.Collections;
using System.ComponentModel;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Authentication;
using DotNetNuke.UI.WebControls;

namespace DotNetNuke.Modules.Articles.Common
{
    /// <summary>
    /// The ArticleSettings class provides a central area for management of Module Configuration Settings.
    /// </summary>
    public class ArticleSettings
    {

        #region Private Members

        private bool _Configured = Null.NullBoolean;
        private bool _CreatorCanDelete = Null.NullBoolean;
        private int _DefaultArticle = Null.NullInteger;
        private int _DefaultSection = Null.NullInteger;
        private DefaultView _DefaultView = DefaultView.ViewSection;
        private int _ModuleID = Null.NullInteger;
        private string _ModuleSkin = Null.NullString;
        private int _TabModuleID = Null.NullInteger;
        private bool _UseModeration = Null.NullBoolean;

        private const string CACHEKEY = "dnn_Articles_Settings";

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor for the ArticleSettings class
        /// </summary>
        /// <remarks>The Constructor is protected forcing the class to be constructed by calling the
        /// static GetSettings factory method</remarks>
        protected ArticleSettings(int moduleId, int tabModuleId)
        {
            ModuleID = moduleId;
            TabModuleID = tabModuleId;

            try
            {
                ModuleController controller = new ModuleController();

                Hashtable moduleSettings = controller.GetModuleSettings(moduleId);
                _Configured = Boolean.Parse(Convert.ToString(moduleSettings[Constants.SETTING_Configured]));
                _CreatorCanDelete = Boolean.Parse(Convert.ToString(moduleSettings[Constants.SETTING_CreatorCanDelete]));
                _UseModeration = Boolean.Parse(Convert.ToString(moduleSettings[Constants.SETTING_UseModeration]));

                Hashtable tabModuleSettings = controller.GetTabModuleSettings(tabModuleId);
                _DefaultView = (DefaultView)Enum.Parse(typeof(DefaultView), tabModuleSettings[Constants.SETTING_DefaultView].ToString());
                _DefaultArticle = Int32.Parse(Convert.ToString(tabModuleSettings[Constants.SETTING_DefaultArticle]));
                _DefaultSection = Int32.Parse(Convert.ToString(tabModuleSettings[Constants.SETTING_DefaultSection]));
                _ModuleSkin = Convert.ToString(tabModuleSettings[Constants.SETTING_ModuleSkin]);
            }
            catch
            {
            }
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets whether the module has been configured
        /// </summary>
        public bool Configured
        {
            get { return _Configured; }
            set { _Configured = value; }
        }

        /// <summary>
        /// Gets and sets whether the Creator of an Article can delete the Article
        /// </summary>
        public bool CreatorCanDelete
        {
            get { return _CreatorCanDelete; }
            set { _CreatorCanDelete = value; }
        }

        /// <summary>
        /// Gets and sets the default Article to display when no Article is selected
        /// </summary>
        public int DefaultArticle
        {
            get { return _DefaultArticle; }
            set { _DefaultArticle = value; }
        }

        /// <summary>
        /// Gets and sets the Default Section to display when no section is selected
        /// </summary>
        public int DefaultSection
        {
            get { return _DefaultSection; }
            set { _DefaultSection = value; }
        }

        /// <summary>
        /// Gets and sets the Default View to display
        /// </summary>
        public DefaultView DefaultView
        {
          get { return _DefaultView; }
          set { _DefaultView = value; }
        }

        /// <summary>
        /// Gets and sets the Module ID this settings class represents
        /// </summary>
        [Browsable(false)]
        public int ModuleID
        {
            get { return _ModuleID; }
            set { _ModuleID = value; }
        }

        /// <summary>
        /// Gets and sets the Module Skin (Template) for this instance
        /// </summary>
        public string ModuleSkin
        {
            get { return _ModuleSkin; }
            set { _ModuleSkin = value; }
        }

        /// <summary>
        /// Gets and sets the Tab Module ID this settings class represents
        /// </summary>
        [Browsable(false)]
        public int TabModuleID
        {
            get { return _TabModuleID; }
            set { _TabModuleID = value; }
        }

        /// <summary>
        /// Gets and sets whether this Module instance uses Moderation
        /// </summary>
        public bool UseModeration
        {
            get { return _UseModeration; }
            set { _UseModeration = value; }
        }


        #endregion

        #region Public Static Methods

        /// <summary>
        /// ClearSettings clears the currently cached settings
        /// </summary>
        /// <param name="tabModuleId">The Tab Module ID</param>
        public static void ClearSettings(int tabModuleId)
        {

            string key = CACHEKEY + "_" + tabModuleId.ToString();
            DataCache.RemoveCache(key);
        }

        /// <summary>
        /// GetSettings retrievese the Settings object for a Module Instance
        /// </summary>
        /// <param name="moduleId">The Module ID</param>
        /// <param name="tabModuleId">The Tab Module ID</param>
        /// <returns></returns>
        public static ArticleSettings GetSettings(int moduleId, int tabModuleId)
        {

            string key = CACHEKEY + "_" + tabModuleId.ToString();
            ArticleSettings settings = DataCache.GetCache(key) as ArticleSettings;

            if (settings == null)
            {
                settings = new ArticleSettings(moduleId, tabModuleId);
                DataCache.SetCache(key, settings);
            }
            return settings;
        }

        /// <summary>
        /// UpdateSettings updates the Settings object for a Module Instance
        /// </summary>
        /// <param name="settings">The ArticleSettings to update</param>
        public static void UpdateSettings(ArticleSettings settings)
        {
            ModuleController controller = new ModuleController();

            controller.UpdateModuleSetting(settings.ModuleID, Constants.SETTING_Configured, settings.Configured.ToString());
            controller.UpdateModuleSetting(settings.ModuleID, Constants.SETTING_CreatorCanDelete, settings.CreatorCanDelete.ToString());
            controller.UpdateModuleSetting(settings.ModuleID, Constants.SETTING_UseModeration, settings.UseModeration.ToString());

            int defView = (int)settings.DefaultView;
            controller.UpdateTabModuleSetting(settings.TabModuleID, Constants.SETTING_DefaultView, defView.ToString());
            controller.UpdateTabModuleSetting(settings.TabModuleID, Constants.SETTING_DefaultArticle, settings.DefaultArticle.ToString());
            controller.UpdateTabModuleSetting(settings.TabModuleID, Constants.SETTING_DefaultSection, settings.DefaultSection.ToString());
            controller.UpdateTabModuleSetting(settings.TabModuleID, Constants.SETTING_ModuleSkin, settings.ModuleSkin);

            ClearSettings(settings.TabModuleID);
        }

        #endregion

    }
}
